<?php
//分类处理类
namespace Org\Yike;
class Category{
	
	//组合一维数组
	Static public function unLimitedForLevel($cate,$html='--',$pid=0,$level=0){
		$arr=array();
		foreach($cate as $v){
			if($v['pid']==$pid){
				$v['level']=$level + 1;
				$v['html']=str_repeat($html,$level);
				$arr[]=$v;
				$arr=array_merge($arr,self::unLimitedForLevel($cate,$html,$v['id'],$level+1));
			}
		}
		return $arr;
	}
	
	//组合多维数组 
	Static public function umLimitedForLayer($cate,$name='child',$pid=0){
		$arr=array();
		foreach ($cate as $v){
			if($v['pid']==$pid){
				$v[$name]=self::umLimitedForLayer($cate,$name,$v['id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}
	
	//传递一个子级返回父级id 例如:首页>>服装>>女装>>裙子
	Static public function getParents($cate,$id){
		$arr=array();
		foreach($cate as $v){
			if($v['id']==$id){
				$arr[]=$v;
				$arr=array_merge(self::getParents($cate,$v['pid']),$arr);
			}
		}
		return $arr;
	}
	
	//传递一个父级ID返回所有子级分类
	Static public function getChiIds($cate,$pid,$str=0){
		$arr=array();
        static $strarr = array();
		foreach ($cate as $v){
			if($v['pid']==$pid){
				$arr[]= $v;
                $strarr[] = $v['id'];
				$arr=array_merge($arr,self::getChiIds($cate,$v['id']));
			}
		}
		return $str == 1 ? $strarr : $arr;
	}
    //获取指定父id的所有分类
    static public function getCateByPid($cate,$pid=0){
        $arr = array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $arr[] = array('id'=>$v['id'],'name'=>$v['name']);
            }
        }
        return $arr;
    }
    //生成树形结构数组
    static public function getCateTreeArr($cate,$pid){
        $arr = array();
        foreach($cate as $v){
            if($v['pid']==$pid){
                $child = Category::getCateTreeArr($cate,$v['id']);
                if($child){
                    $v['child'] = $child;
                }
                $arr[] = $v;

            }

        }
        return $arr;
    }
}

?>
